class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        ans = []
        n = len(nums)
        nums.sort()

        for x in range(n):
            if x != n-1 and x != 0 and nums[x] == nums[x-1]:continue
            
            for y in range(x+1, n):
                if y != n-1 and y !=x+1 and nums[y] == nums[y-1]:continue

                l, r = y+1, n-1

                while l < r:
                    sum = nums[x] + nums[y] + nums[l] + nums[r]
                    if sum == target:
                        ans.append([nums[x], nums[y], nums[l], nums[r]])
                        print(x, y , l, r)
                        l += 1
                        r -= 1
                        while l < r and nums[l] == nums[l-1]:
                            l += 1
                        while l < r and nums[r] == nums[r+1]:
                            r -= 1
                    elif sum > target:
                        r -= 1
                    else:
                        l += 1
        return ans